<template>
  <div>
    <el-card class="box-card">
      <div>
        <span class="iconfont icon-dingdan"></span>
        <span>商品信息数据列表</span>
      </div>
      <span>
        <el-button type="text" @click="open">
          <button>添加</button>
        </el-button></span
      >
    </el-card>
    <el-table :data="productlist" border style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>

      <el-table-column prop="name" label="类型名称"> </el-table-column>

      <el-table-column prop="param_count" label="属性数量"> </el-table-column>
      <el-table-column prop="show_type" label="参数数量"> </el-table-column>

      <el-table-column label="操作">
           <template slot-scope="scope">  
             <el-button type="text" @click="opent(scope.row.name,scope.row.id)">
          <button>编辑</button>
        </el-button>
          <el-button type="text" @click="openr(scope.row.id)">
          <button>删除</button>
        </el-button>
           </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="listQuery.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { fetchProductAttr, addAttr,deleteAttr,updatedAttr} from "@/api/product";
export default {
  name: "productAttr",
  data: function () {
    return {
      productlist: [],
      total: 0, //总数量
      name: "",
      listQuery: {
        pageSize: 5,
        pageNum: 1,
      },
    };
  },
  mounted() {
    //调用函数
    this.getProductAttrData();
  },
  methods: {
    //添加弹窗
    open() {
      this.$prompt("请输入类型名称", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          addAttr({ name: value }).then((res) => {
            if (res.status === 200) {
              this.$message({
                type: "success",
                message: "你的商品类型: " + value,
              });
              this.getProductAttrData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    //编辑弹窗
     opent(name,id) {
      this.$prompt("*类型名称", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputValue:name
      })
        .then(({ value }) => {
          updatedAttr({ name: value,id:id}).then((res) => {
            if (res.status === 200) {
              this.$message({
                type: "success",
                message: "你的商品类型: " + value,
              });
              this.getProductAttrData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    //删除弹窗
      openr(id) {
        console.log(id);
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteAttr({id}).then((res)=>{
              if(res.status === 200){
                this.$message({
                  type: 'success',
                   message: '删除成功!'
                })
                this.getProductAttrData()
              }
            })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },

    //修改每页显示的条数
    handleSizeChange(val) {
      // console.log("修改每页显示的条数 触发了", val);
      if (val) {
        this.listQuery.pageSize = val;
        this.listQuery.pageNum = 1; //页面换个数时重新从第一页开始
      }
      //重新获取数据
      this.getProductAttrData();
    },
    //修改当前页的页码
    handleCurrentChange(val) {
      // console.log("修改当前页的页码 触发了", val);
      if (val) {
        this.listQuery.pageNum = val;
      }
      this.getProductAttrData();
    },
    async getProductAttrData() {
      var res = await fetchProductAttr(this.listQuery);
      this.productlist = res.data;
      this.total = res.total;
      console.log(this.productlist);
    },
  },
};
</script>

<style>
.el-card__body {
  padding: 0;
  align-items: center;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
</style>